package org.xshop.web;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.xshop.core.ExpressmanHelper;
import org.xshop.model.TExpressmanAccount;
import org.xshop.service.ITExpressmanAccountService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 骑手交易账户  前端控制器
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-05-30
 */
@Controller
@RequestMapping("/app/expressman/")
@Api(tags = "ExpressmanAccount", value = "骑手交易账户接口", description = "骑手交易账户接口")
public class TExpressmanAccountController extends AppBaseController<TExpressmanAccount, ITExpressmanAccountService> {
    @ApiOperation(value = "查询骑手交易账户", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "accountList.api", method = {RequestMethod.GET, RequestMethod.POST})
    public Object query(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        param.put("expressmanId", ExpressmanHelper.getCurrent(request));
        return super.query(param);
    }

    @PostMapping("addAccount.api")
    @ApiOperation(value = "新增骑手交易账户", notes = "accountType:1微信2支付宝", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object update(HttpServletRequest request, String accountType, String accountNo) {
        TExpressmanAccount param = WebUtil.getParameter(request, TExpressmanAccount.class);
        Long expressmanId = ExpressmanHelper.getCurrent(request);
        param.setExpressmanId(expressmanId);
        if (param.getId() == null) {
            param.setCreateBy(expressmanId);
            param.setCreateTime(new Date());
        }
        param.setUpdateBy(expressmanId);
        param.setUpdateTime(new Date());
        service.update(param);
        return setSuccessModelMap();
    }

    @PostMapping("pay.api")
    @ApiOperation(value = "交易账户支付验证下单", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object pay(HttpServletRequest request, String accountType, String accountNo) {
        TExpressmanAccount param = WebUtil.getParameter(request, TExpressmanAccount.class);
        param.setExpressmanId(ExpressmanHelper.getCurrent(request));
        param.setOpenid(WebUtil.getHost(request));
        Object result = service.pay(param);
        return setSuccessModelMap(result);
    }

    @PostMapping("delAccount.api")
    @ApiOperation(value = "删除骑手交易账户", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object delete(HttpServletRequest request, String accountType, String accountNo) {
        TExpressmanAccount param = WebUtil.getParameter(request, TExpressmanAccount.class);
        param.setExpressmanId(ExpressmanHelper.getCurrent(request));
        service.deleteByEntity(param);
        return setSuccessModelMap();
    }
}
